﻿package com.game.module.heroBuff.view
{
	import com.game.module.heroBuff.vo.SingleSavedBuff;
	
	import config.Config;
	
	import flash.display.Sprite;
	
	public class SavedBuff extends Sprite
	{
		
		public function SavedBuff()
		{
			gc = Config.getIns();
		}
		
		private var buffArray:Array = [];
		private var gc:Config;
		
		public function step():void
		{
			var idx:int;
			var temp:Array = [];
			for each (var ssb:SingleSavedBuff in this.buffArray)
			{
				if (!ssb.isDestroyed)
				{
					ssb.step();
				}
				if (ssb.isDestroyed)
				{
					temp.push(ssb);
				}
			}
			if (temp.length > 0)
			{
				for each (ssb in temp)
				{
					idx = this.buffArray.indexOf(ssb);
					if (!(idx == -1))
					{
						this.buffArray.splice(idx, 1);
					}
				}
				this.refreshDisplay();
			}
		}
		
		public function setFromMemory(data:String):void
		{
			var dataArr:* = null;
			var len:uint;
			var i:uint;
			var str:* = null;
			var arr:* = null;
			var _loc_9:uint;
			var value:int;
			var time:int;
			if (!data || data == "")
			{
				return;
			}
			if (this.buffArray)
			{
				for each (var ssb:SingleSavedBuff in this.buffArray)
				{
					if (this.contains(ssb))
					{
						this.removeChild(ssb);
					}
				}
				this.buffArray.length = 0;
				dataArr = data.split(",");
				if (dataArr.length > 0)
				{
					len = dataArr.length;
					i = 0;
					while (i < len)
					{
						str = dataArr[i];
						arr = str.split("|");
						if (arr.length == 3)
						{
							_loc_9 = arr[0];
							value = arr[1];
							if (_loc_9 == SingleSavedBuff.BUFF_EXP_ADD)
							{
								if (value > 3)
								{
									value = 3;
								}
							}
							else
							{
								if (_loc_9 == SingleSavedBuff.BUFF_ATK_ADD)
								{
									if (value > 300)
									{
										value = 300;
									}
								}
								else
								{
									if (_loc_9 == SingleSavedBuff.BUFF_MID_BUFF)
									{
										if (value > 500)
										{
											value = 500;
										}
									}
								}
							}
							time = arr[2];
							if (time > 3600 * gc.frameClips)
							{
								time = 3600 * gc.frameClips;
							}
							this.addBuff(_loc_9, value, time);
						}
						i++;
					}
				}
			}
		}
		
		public function getMemoryString():String
		{
			var i:uint;
			var ssb:* = null;
			var returnString:String = "";
			var len:uint = this.buffArray.length;
			i = 0;
			while (i < len)
			{
				ssb = (this.buffArray[i] as SingleSavedBuff);
				returnString += ssb.type + "|" + ssb.value + "|" + ssb.time;
				if (i < len - 1)
				{
					returnString += ",";
				}
				i++;
			}
			return returnString;
		}
		
		public function addBuff(param1:uint, value:int, time:int):void
		{
			var isNew:Boolean;
			var ssb:* = null;
			if (time > 0)
			{
				isNew = true;
				for each (ssb in this.buffArray)
				{
					if (ssb.type == param1)
					{
						isNew = false;
						ssb.value = value;
						ssb.time = time;
						break;
					}
				}
				if (isNew)
				{
					ssb = new SingleSavedBuff(param1, value, time, this.buffArray.length);
					this.addChild(ssb);
					this.buffArray.push(ssb);
				}
			}
			//gc.savedBuffString = gc.savedBuff.getMemoryString();
		}
		
		public function getBuffByType(param1:uint):SingleSavedBuff
		{
			for each (var ssb:SingleSavedBuff in this.buffArray)
			{
				if (ssb.type == param1)
				{
					return ssb;
				}
			}
			return null;
		}
		
		public function removeBuffByType(param1:uint):void
		{
			for each (var ssb:SingleSavedBuff in this.buffArray)
			{
				if (ssb.type == param1)
				{
					ssb.destroy();
					return;
				}
			}
		}
		
		public function destroy():void
		{
			if (this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		
		private function refreshDisplay():void
		{
			var idx:int;
			for each (var ssb:SingleSavedBuff in this.buffArray)
			{
				idx = this.buffArray.indexOf(ssb);
				ssb.idx = idx;
			}
		}
		
		
	}
}//package my

